<?php
if( !defined( 'FORMS' ) ): define( 'FORMS', 1 );
if( !defined( 'FILE_ROOT' ) ) include_once( '../../_prerun' );

USE_LIB('formfield');
USE_LIB('formfieldinput');
USE_LIB('formfieldpassword');
USE_LIB('formfieldhidden');
USE_LIB('formfieldemail');
USE_LIB('formfieldtext');
USE_LIB('formfieldhtmltext');
USE_LIB('formfieldnumber');
USE_LIB('formfieldhttp');
USE_LIB('formfieldfile');
USE_LIB('formfieldinlinefile');
USE_LIB('formfieldpicture');
USE_LIB('formfieldcheckbox');
USE_LIB('formfieldmulticheckbox');
USE_LIB('formfieldoption');
USE_LIB('formfieldselect');
USE_LIB('formfieldrichselect');
USE_LIB('formfieldrichcollection');
USE_LIB('formfieldrichselectcollection');
USE_LIB('formfieldcountry');
USE_LIB('formfieldcivilite');
USE_LIB('formfielddate');
USE_LIB('formfieldtime');
USE_LIB('formfieldrichtext');
USE_LIB('formfieldmediatext');
USE_LIB('formfieldaddress');
USE_LIB('form');
USE_LIB('formprocessor');

final class Forms
	{
	static function getField( $name, $values )
		{
		$db_type = $values['type'];
		$needed = $values['needed']==1 ? 'true' : 'false';
		if( self::isLongText( $name, $db_type ) )
			{
			eval("\$obj = new FormFieldText('$name',$needed);\n");
			}
		else
		if( self::isShortText( $name, $db_type ) )
			{
			eval("\$obj = new FormFieldInput('$name',$needed);\n");
			}
		else
		if( self::isNumber( $name, $db_type ) )
			{
			eval("\$obj = new FormFieldNumber('$name',$needed);\n");
			}
		else
		if( self::isDate( $name, $db_type ) )
			{
			eval("\$obj = new FormFieldDate('$name',$needed);\n");
			}
		else
		if( self::isBoolean( $name, $db_type ) )
			{
			eval("\$obj = new FormFieldCheckbox('$name',$needed);\n");
			}
		else
		if( self::isFile( $name, $db_type ) )
			{
			eval("\$obj = new FormFieldFile('$name',$needed);\n");
			}
		else
		if( self::isA( $name, $db_type ) )
			{
			eval("\$obj = new FormFieldSelect('$name',$needed);\n");
			}
		$obj->setDatabaseTypes( $values );
		return $obj;
		}
		
	static function isLongText( $name, $type )
		{
		if( stripos( $type, 'text' ) !== false )
			{
			return !self::isShortText( $name, $type );
			}
		return false;
		}
		
	static function isShortText( $name, $type )
		{
		if( stripos( $type, 'tinytext' ) !== false )
			{
			return true;
			}
		else
		if( stripos( $type, 'varchar' ) !== false )
			{
			if( !self::isFile( $name, $type ) ) return true;
			}
		return false;
		}

	static function isNumber( $name, $type )
		{
		if( ( stripos( $type, 'int' ) !== false || stripos( $type, 'float' ) !== false ) && $name != 'id' )
			{
			return !self::isDate( $name, $type ) && !self::isBoolean( $name, $type ) && !self::isA( $name, $type );
			}
		return false;
		}

	static function isDate( $name, $type )
		{
		return( stripos( $type, 'int(14)' ) !== false );
		}

	static function isBoolean( $name, $type )
		{
		return( stripos( $type, 'int(1)' ) !== false );
		}

	static function isFile( $name, $type )
		{
		if( stripos( $type, 'varchar(255)' ) !== false )
			{
			if( stripos( $name, 'file' ) != false || stripos( $name, 'picture' ) != false )
				{
				return true;
				}
			}
		return false;
		}

	static function isPicture( $name, $type )
		{
		if( stripos( $type, 'varchar(255)' ) !== false )
			{
			if( stripos( $name, 'picture' ) != false )
				{
				return true;
				}
			}
		return false;
		}

	static function isA( $name, $type )
		{
		if( stripos( $type, 'int' ) !== false && stripos( $name, 'id' ) > 0 )
			{
			USE_LIB('Database');
			$db = Database::getInstance();
			$search = preg_replace( "/f_(.*)_id/mis", "$1", $name );
			$tables = $db->query2array( 'show tables from ' . MYSQL_DBNAME );
			foreach( $tables as $table => $elt )
				{
				if( $elt['Tables_in_'.MYSQL_DBNAME] == $search ) return true;
				}
			}
		return false;
		}
	}

endif;
?>